


/*This program is for preparing ORGs from 1996-2010. It takes the IPUMS harmonized and unharmonized ORG data,
creates income and earnings variables used in the analysis. Imputation of hours where necessary. All cases are included here,
and recodes occur prior to linking longitudinally.
*/ 

clear
set more off
local indir "../replication-package"
local racebridge "data/setup/02_racebridge.dta"
local empstat "data/setup/02_empstat.dta" /* identify output data file, e.g. racebridge.dta */ 


capture cd "`indir'"

use "`racebridge'", clear

*capture drop *PROB*

/* code morg data availability */

* eligorg flag: indicates ORG data from 1982 onwards
	* NBER: ORG datafiles as far back as 1979 


/* note, recode empstat and morg eligibility earlier */
	/* NIU codes aren't correct in 1989-1993, coded as 999999 instead 9999.99 */
*** NOTE need to look at NIU codes for the unharmonized variables or create a new variable first ****/	
	*** look at topcodes 
	*** NEED TO RUN THIS CODE AFTER THE EARNSTAT VARIABLE 

* Code no longer needed because I create a new variable including only valid responses (earnweek82on and an earnstat variable indication availability)	
/*	
foreach var of varlist earnweek* {
  replace `var' = . if (`var' == 9999.99 | `var' == 999999)
  }
*/

gen empstat3=empstat
	recode empstat3 (10=1)(12=1)(13=1)(20=2)(21=2)(22=2)(30/36=3)
	label define empstat3_lbl 0 "NIU", add
	label define empstat3_lbl 1 "Employed", add
	label define empstat3_lbl 2 "Unemployed", add
	label define empstat3_lbl 3 "Not in Labor Force", add
	label values empstat3 empstat3_lbl
	tab year empstat3 if age>=16
	
	label var empstat3 "Employment and LF status"

  	//label define empstat3_lbl 0 "NIU" 1 "Employed" 2 "Unemployed" 3 "Not in Labor Force"

	label val empstat3 empstat3_lbl
  
  
* eligorg is an existing CPS variable, but includes some individuals who are flagged as eligible but have NIU incomes
	* note, there used to be NIUs for 1989-1993, but they no longer are included

* dummy for valid earnings data: 0 = not eligible, 1 = eligible no data, 2 = eligible with data 
gen earnstat = eligorg
	replace earnstat = 0 if year < 1982
	replace earnstat = 2 if (year >=1982 & eligorg == 1 & earnweek != 9999.99)
	*replace earnstat = 2 if (year >=1982 & year < 1989 & eligorg ==1 & uh_ernwkc_1 > -999)
	
	label var earnstat "ORG earnings data status"
	label def earnstat 0 "0: Not eligible" 1 "1: Eligible, earnings NIU " 2 "2: Valid earnings data", replace
	label val earnstat earnstat
	
	tab earnstat
	tab year earnstat, missing
	tab earnstat eligorg, missing

/* I can identify AF through 1982, ASEC identifies going back further */

* identify armed forces, not eligible for org
	* 75% of the AF below are coded based on empstat, remainder have another empstat (mostly NIU) 
	* are you ineligible for the morg based on classwkr and occ, or just empstat

gen af = empstat == 1 | classwkr==26 | occ1990==905
	label var af "In armed forces"

* identify self-employed, not org eligible
	* includes unpaid family workers
gen se = inlist(classwkr, 10,13,14,29)
	label var se "Self-employed"

* gen working, eligible for org
gen employed = empstat == 10 | empstat == 11	
	label var employed "Has job"
	
* note, there are a couple of SE people in 1992 that have earnings data
tab earnstat if af==1 | se==1 

gen orgelig = eligorg
	replace orgelig = 2 if (mis !=4 & mis !=8) | year < 1982
	replace orgelig = 3 if employed == 0 
	replace orgelig = 4 if se == 1
	replace orgelig = 5 if af == 1
	* About 5000 cases are employed, but coded as not eligible, all in 1982
	replace orgelig = 6 if orgelig == 0
	
	label var orgelig "ORG eligibility"
	label def orgelig 1 "1: Eligible" 2 "2: Not ORG month or year" 3 "3: Not employed" 4 "4: Self-employed" 5 "5: Armed forces" 6 "6: Unknown reason", replace
	label val orgelig orgelig
	
	tab orgelig, missing
	tab year orgelig


* EARNWEEK is now available harmonized from 1982+ following code isn't necessary	
	
/*	
* new earnweek variable combining harmonized EARNWEEK (1989+) with unharmonized uh_ernwkc_1 (1982-1988)
gen earnweek82on = earnweek
	replace earnweek82on = uh_ernwkc_1 if year >=1982 & year <=1988
	replace earnweek82on = . if earnstat !=2
	
	label var earnweek82on "ORG earnings 1982-present"
	
	summ earnweek82on, d
*/

* paidhour: available 1982+ for org eligibles, including those with missing earnings data	
	* no need for recoding


* UHRSWORKORG is usual hours at main job, so will only include main job pre 1989 or when no answer for org variable	
	* create indicator variable for source of hour data
	gen workhrstat = .
	
	* create new hours variable based on ORG
	gen workhr82on = .
		
		* workers with reported usual hours
		* if use ORG variable (1989+ paid hourly), flag source as 1
		replace workhr82on = uhrsworkorg if year >=1989 & orgelig == 1 & uhrsworkorg < 100 
		replace workhrstat = 1 if year >=1989 & orgelig == 1 & uhrsworkorg < 100
		
		* 1989+ but uhursworkorg missing or NIU or hours vary (?) and valid uhrswork1
		replace workhr82on = uhrswork1 if year >=1989 & orgelig == 1 & uhrsworkorg >= 100 & uhrswork1 < 100
		replace workhrstat = 2 if year >=1989 & orgelig == 1 & uhrsworkorg >= 100 & uhrswork1 < 100
		
		* 1982-1988 with valid uhrswork1
		replace workhr82on = uhrswork1 if year < 1989 & orgelig == 1 & uhrswork1 < 100
		replace workhrstat = 2 if year < 1989 & orgelig == 1 & uhrswork1 < 100 
		
		* workers whose hours vary
		* all years, full time work schedules, no info on usual hours (some may usually work full-time), use hours last week
		replace workhrstat = 3 if orgelig == 1 & workhr82on == . & wkstat >=10 & wkstat < 20  
		replace workhr82on = ahrswork1 if orgelig == 1 & workhr82on == . & wkstat >=10 & wkstat < 20  
		
		* all years, part-time work schedules, no info on usual hours, use hours last week
		replace workhrstat = 4 if orgelig == 1 & workhr82on == . & wkstat >=20  
		replace workhr82on = ahrswork1 if orgelig == 1 & workhr82on == . & wkstat >=20  		
		
	tab1 workhr82on workhrstat if orgelig == 1, missing
	
	label var workhr82on "Hours worked (usual or last week)"
	label var workhrstat "Source of work hours"
	
	label def workhrstat 0 "not eligible" 1 "usual hrs ORG (1989+)" 2 "usual hrs basic" 3 "hrs last week, FT varies" 4 "hrs last week, PT varies"
	label val workhrstat workhrstat

		
save "`empstat'", replace


